package com.example.myandroidapp1;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myandroidapp1.activity.AbstractBaseActivity;
import com.example.myandroidapp1.adapter.GalleryAdapter;
import com.example.myandroidapp1.entity.Gallery;
import com.example.myandroidapp1.factory.GalleryServiceFactory;
import com.example.myandroidapp1.service.GalleryViewService;

public class GalleryViewActivity extends AbstractBaseActivity implements
		OnItemClickListener {

	private static final String URL = "http://166.78.125.197:8080/primefaces/app/json";
	//private static final String URL = "http://localhost:8080/primefaces-showcase/app/json";
	
	private GridView gridView;
	private Gallery gallery;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.galleryview);
		initialize();
		gridView.setAdapter(new GalleryAdapter(this, gallery));
		gridView.setOnItemClickListener(this);

	}

	private void initialize() {
		GalleryViewService galleryViewService = GalleryServiceFactory
				.getInstance();
		try {
			gallery = galleryViewService.execute(URL).get();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}// call the service to get the gallery
		gridView = (GridView) findViewById(R.id.galleryView);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
		ImageView imageView = (ImageView) v.findViewById(R.id.grid_item_image);
		TextView hiddenTextView = (TextView) v
				.findViewById(R.id.tvGridItemHidden);
		// myGallery.setCurrentPosition(pos);
		Bundle bundle = new Bundle();
		// bundle.putSerializable("myGallery", myGallery);
		Intent i = new Intent(this, GridViewImage.class);
		i.putExtras(bundle);
		startActivity(i);

	}

	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}

}
